package com.deep.transaction.demo7.service.impl;

import com.deep.transaction.demo7.dao.UserDao;
import com.deep.transaction.demo7.model.User;
import com.deep.transaction.demo7.service.UserService;
import com.deep.transaction.demo7.service.UserService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("demo7UserService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    public UserService2 userService2;


    @Autowired
    public ApplicationContext applicationContext;

    /**
     * 正常测试事务
     */
    @Override
    @Transactional
    public void test01(User user, User user2) {
        userDao.updateByPrimaryKeySelective(user);
        int i = 1 / 0;
        userDao.updateByPrimaryKeySelective(user2);
    }

    /**
     * 测试事务自调用
     */
    public void test02(User user) {
        testTransac(user);
    }

    @Transactional
    public void testTransac(User user) {
        userDao.insert(user);
        int i = 1 / 0;
    }


    @Transactional
    public void testRequired(User user) {
        try {
            userService2.testRequired(user);
        } catch (Exception e) {

        }
//        userService2.testRequired(user);
        user.setAge(25);
//        int i = 1/0;
        userDao.updateByPrimaryKeySelective(user);
    }

    @Transactional
    public void testSupports(User user) {
        try {
            userService2.testSupports(user);
        } catch (Exception e) {

        }
//        userService2.testSupports(user);
        user.setAge(25);
//        int i = 1/0;
        userDao.updateByPrimaryKeySelective(user);
    }

    @Transactional
    public void testMandatory(User user) {
//        userService2.testMandatory(user);
        try {
            userService2.testMandatory(user);
        } catch (Exception e) {
            // TODO: handle exception
        }
        user.setAge(25);
//        int i = 1/0;
        userDao.updateByPrimaryKeySelective(user);
    }

    @Transactional
    public void testRequiresNew(User user, User user2) {
        userDao.insert(user2);
        userService2.testRequiresNew(user);
        /* try
        {
            userService2.testRequiresNew(user);
        } catch (Exception e)
        {
            // TODO: handle exception
        }*/
        int i = 1 / 0;
        user2.setName("王五2");
        userDao.updateByPrimaryKeySelective(user2);
    }

    @Transactional
    public void testNotSupported(User user, User user2) {
        userDao.insert(user2);
        userService2.testNotSupported(user);
        /*try
        {
            userService2.testNotSupported(user);
        } catch (Exception e)
        {
            // TODO: handle exception
        }*/
        int i = 1 / 0;
        user2.setName("王五2");
        userDao.updateByPrimaryKeySelective(user2);
    }

    //    @Transactional
    public void testNever(User user, User user2) {
        userDao.insert(user2);
        userService2.testNever(user);
        user2.setName("王五2");
        userDao.updateByPrimaryKeySelective(user2);
    }

    @Transactional
    public void testNested(User user, User user2) {
        userDao.insert(user2);
        userService2.testNested(user);
        /*try
        {
            userService2.testNested(user);
        } catch (Exception e)
        {
            // TODO: handle exception
        }*/
//        int i = 1/0;
        user2.setName("王五2");
        userDao.updateByPrimaryKeySelective(user2);
    }


}
